#include <stdio.h>

int reverse_file (FILE * rfp, FILE *wfp);

int main (int argc, char **argv)
{
	FILE *fp1, *fp2;
	if (argc < 3) {
		return -1;
	}

	fp1 = fopen (argv[1], "r");
	if (NULL == fp1) {
		fprintf (stderr, "fopen() failed!\n");
		return -1;
	}

	fp2 = fopen (argv[2], "w");
	if (NULL == fp2) {
		fprintf (stderr, "fopen() failed!\n");
		return -1;
	}
	reverse_file (fp1, fp2);
	fclose (fp1);
	fclose (fp2);
	return 0;
}

int reverse_file (FILE * rfp, FILE *wfp)
{
	long int size = 0;
	int i = 1;
	char c = 0;
	//定位到源文件流的末尾
	fseek (rfp, 0, SEEK_END);
	size = ftell (rfp);
	
	//定位到目标文件的文件流开头
	rewind (wfp);
	
	while (i <= size) {
		fseek (rfp, size - i, SEEK_SET);
		c = fgetc (rfp);
		if (c == EOF) {
			if (ferror (rfp)) {
				return -1;
			}
		}
		fputc (c, wfp);
		i++;
	}
	return 0;
}

